﻿using System.Data;
using Azure;
using Microsoft.Data.SqlClient;

namespace HRMSystemShi.DAL
{
    public class SqlHelper
    {
        private static string? connStr = "Server=(localdb)\\MSSQLLocalDB; Database=HRMDB; User Id = hrmtest; Password=hrmtest;";
        public static SqlDataReader ExecuteReader(string sql,params SqlParameter[] sqlParameters)
        {
            SqlConnection conn = new SqlConnection(connStr);
            SqlCommand cmd=new SqlCommand(sql,conn);
            cmd.Parameters.AddRange(sqlParameters);
            conn.Open();
            return cmd.ExecuteReader(CommandBehavior.CloseConnection);
        }
        public static int ExecuteNonQuery(string sql, params SqlParameter[] sqlParameters)
        {
            SqlConnection conn = new SqlConnection(connStr);
            SqlCommand cmd = new SqlCommand(sql,conn);
            cmd.Parameters.AddRange(sqlParameters);
            conn.Open();
            return cmd.ExecuteNonQuery();
        }
        public static DataTable GetDataTable(string sql, params SqlParameter[] sqlParameters)
        {
            DataTable dt = new DataTable();
            SqlConnection conn=new SqlConnection(connStr);
            SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
            sda.SelectCommand.Parameters.AddRange(sqlParameters);
            sda.Fill(dt);
            return dt;
        }
        public static object ExecuteScalar(string sql, params SqlParameter[] sqlParameters)
        {
            SqlConnection conn = new SqlConnection(connStr);
            SqlCommand cmd = new SqlCommand(sql,conn);
            cmd.Parameters.AddRange(sqlParameters);
            conn.Open();
            return cmd.ExecuteScalar();
        }
    }
}
